﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace ConsoleApplication1
{
    public class SweetPrinter
    {
        public const string FilePrinter = "file";
        public const string ConsolePrinter = "textBox";

        public static IPrinter GetPrinterFromTypes(string type)
        {
            IPrinter printer;
            switch (type)
            {
                case FilePrinter:
                    printer = new PresentFilePrinter();
                    break;
                case ConsolePrinter:
                    printer = new PresentBoxPrinter();
                    break;
                default: throw new ArgumentException("Неверный тип вывода");
            }
            return printer;
        }
    }

    public class PresentBoxPrinter : IPrinter
    {
        public void Print(Present presents, ListBox SweetListBox)
        {
            List<AbstractSweet> sweets = presents.GetSweets();
            SweetListBox.Items.Clear();

            int count = 1;
            foreach (AbstractSweet pr in sweets)
            {
                SweetListBox.Items.Add(count + ")   " + pr.Name + " " + pr.Weight + Environment.NewLine);
                count++;
            }

        }
    }

    public class PresentFilePrinter : IPrinter
    {
        public const string FileName = "outputLog.txt";
        SweetCalculator calc = new SweetCalculator();

        public void Print(Present present, ListBox SweetListBox)
        {
            List<AbstractSweet> sweets = present.GetSweets();

            try
            {
                using (StreamWriter writer = new StreamWriter(FileName))
                {
                    foreach (AbstractSweet pr in sweets)
                    {
                        writer.WriteLine(pr.Name + " " + pr.Weight);
                    }

                    double totalWeight = calc.GetTotalWeight(present);
                    writer.WriteLine("\n" + "Общий вес подарка = " + totalWeight);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Файл не может быть прочитан: ", Encoding.GetEncoding(1251));
                Console.WriteLine(e.Message);
            }
        }
    }
}
